<template>
  <div class="app-container">
    <el-row class="row">
      <div class="action">
        <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="handleCreate">新增绑定</el-button>
      </div>
      <el-table :data="list"  element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label='时间' >
          <template slot-scope="scope">
            {{scope.row.createtime}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="用户名">
          <template slot-scope="scope">
            {{scope.row.username}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="渠道">
          <template slot-scope="scope">
            {{scope.row.cname}}
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center">
          <template slot-scope="scope">
            {{scope.row.bname}}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            {{scope.row.delflagstr}}
          </template>
        </el-table-column>
        <el-table-column align="left" label="操作" width="430" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" >重新绑定</el-button>
            <span v-if="scope.row.delflag==0" class="el-binding-span"><a :href = "scope.row.downloadBaseUrl" >下载基本包</a></span>
            <span v-if="scope.row.delflag==0" class="el-binding-span"><a :href = "scope.row.downloadGzUrl" >下载刷机工具</a></span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.pageNum" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-row>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form  autoComplete="on" :model="bindingForm"  ref="bindingForm"   label-position="left">
        <el-input type="hidden" v-model="bindingForm.id"></el-input>
        <el-form-item label="用户" prop="delflag">
          <el-select v-model="bindingForm.accountid" v-if="dialogStatus=='create'"  placeholder="请选择用户">
            <el-option v-for="item in  userOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
          <el-select v-model="bindingForm.accountid" v-else :disabled="true" placeholder="请选择用户">
            <el-option v-for="item in  userOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="">
          <el-select v-model="bindingForm.brandid" v-if="dialogStatus=='create'"  placeholder="请选择品牌">
            <el-option v-for="item in  brandOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
          <el-select v-model="bindingForm.brandid" v-else :disabled="true" placeholder="请选择品牌">
            <el-option v-for="item in  brandOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="">
          <el-select v-model="bindingForm.channelid" v-if="dialogStatus=='create'"  placeholder="请选择渠道">
            <el-option v-for="item in  channelOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
          <el-select v-model="bindingForm.channelid" v-else :disabled="true" placeholder="请选择渠道">
            <el-option v-for="item in  channelOptions" :key="item.key" :label="item.text" :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">创建</el-button>
        <el-button v-else type="primary" @click="updateData">绑定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import { useroptionList, brandoptionList, channeloptionList, fetchList, saveBinding } from '@/api/binding'
  import { parseTime } from '@/utils/index'
  export default {
    name: 'channel',
    data() {
      return {
        list: [],
        total: null,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 20
        },
        textMap: {
          update: '编辑绑定',
          create: '新建绑定'
        },
        dialogFormVisible: false,
        dialogStatus: '',
        userOptions: [],
        brandOptions: [],
        channelOptions: [],
        statusOptions: [
          { key: 0, text: '绑定成功' },
          { key: 1, text: '绑定失败' }
        ],
        bindingForm: {
          id: '',
          accountid: '',
          brandid: '',
          bname: '',
          channelid: '',
          cname: '',
          delflagstr: '',
          delflag: ''
        }
      }
    },
    created() {
      this.getList()
      this.getUserOption()
      this.getBrandOption()
      this.getChannelOption()
    },
    methods: {
      resetForm() {
        this.bindingForm = {
          id: '',
          accountid: '',
          brandid: '',
          channelid: ''
        }
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val
        this.getList()
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.resetForm()
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['bindingForm'].clearValidate()
        })
      },
      handleUpdate(row) {
        console.log(row)
        this.bindingForm = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['bindingForm'].clearValidate()
        })
      },
      getUserOption() {
        const data = { delflag: 0 }
        useroptionList(data).then(response => {
          if (response.code === 1) {
            const list = response.data
            for (const v of list) {
              const option = {}
              option.key = v.id
              option.text = v.username
              this.userOptions.push(option)
            }
          }
        })
      },
      getBrandOption() {
        const data = { delflag: 0 }
        brandoptionList(data).then(response => {
          if (response.code === 1) {
            const list = response.data
            for (const v of list) {
              const option = {}
              option.key = v.id
              option.text = v.bname
              this.brandOptions.push(option)
            }
          }
        })
      },
      getChannelOption() {
        const data = { delflag: 0 }
        channeloptionList(data).then(response => {
          if (response.code === 1) {
            const list = response.data
            for (const v of list) {
              const option = {}
              option.key = v.id
              option.text = v.cname
              this.channelOptions.push(option)
            }
          }
        })
      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          if (response.code === 1) {
            const list = response.data.list
            this.total = response.data.total
            this.list = []
            for (const v of list) {
              const item = {}
              item.id = v.id
              item.createtime = v.createtime
              item.username = v.account.username
              item.accountid = v.accountid
              item.cname = v.channel.cname
              item.channelid = v.channelid
              item.bname = v.brand.bname
              item.brandid = v.brandid
              item.delflag = v.delflag
              item.downloadBaseUrl = v.downloadBaseUrl
              item.downloadGzUrl = v.downloadGzUrl
              if (v.delflag === 0) {
                item.delflagstr = '绑定成功'
              } else {
                item.delflagstr = '绑定失败'
              }
              this.list.push(item)
            }
            setTimeout(() => {
              this.listLoading = false
            }, 1.5 * 1000)
          }
        })
      },
      createData() {
        saveBinding(this.bindingForm).then(() => {
          const data = this.bindingForm
          const time = parseTime(new Date())
          data.createtime = time
          for (const v of this.userOptions) {
            if (data.accountid === v.key) {
              data.username = v.text
              break
            }
          }
          for (const v of this.brandOptions) {
            if (data.brandid === v.key) {
              data.bname = v.text
              break
            }
          }
          for (const v of this.channelOptions) {
            if (data.channelid === v.key) {
              data.cname = v.text
              break
            }
          }
          data.delflagstr = '绑定成功'
          this.list.unshift(data)
          this.dialogFormVisible = false
          this.$message({
            title: '成功',
            message: '创建成功',
            type: 'success',
            duration: 2000
          })
        })
      },
      updateData() {
        saveBinding(this.bindingForm).then(() => {
          for (const v of this.list) {
            if (v.id === this.bindingForm.id) {
              const index = this.list.indexOf(v)
              this.bindingForm.delflagstr = '绑定成功'
              this.list.splice(index, 1, this.bindingForm)
              break
            }
          }
          this.dialogFormVisible = false
          this.$message({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
      }
    }
  }
</script>

